local BaseData = require("data.base.BaseData")
local UserData = class("UserData", BaseData)

local getSchema = function()
    return {
        {
            name = "base",
            module = require("data.UserBaseData")
        },
        {
            name = "gameCache",
            module = require("data.GameCacheData")
        },
        {
            name = "equipment",
            module = require("data.EquipmentData")
        },
        {
            name = "items",
            module = require("data.ItemsData")
        },
        {
            name = "magickey",
            module = require("data.MagicKeyData")
        },
        {
            name = "gemstone",
            module = require("data.GemstoneData")
        },
        {
            name = "resource",
            module = require("data.ResourceData")
        },
        {
            name = "mail",
            module = require("data.MailData")
        },
        {
            name = "serverTime",
            module = require("data.ServerTimeData")
        },
        {
            name = "hero",
            module = require("data.HeroData")
        },
        {
            name = "battle",
            module = require("data.BattleData")
        },
        {
            name = "chat",
            module = require("data.ChatData")
        },
        {
            name = "teamGroup",
            module = require("data.TeamGroupData")
        },
        {
            name = "gameLogic",
            module = require("data.GameLogicData")
        }
    }
end

local schema = {}

local generateSchema = function()
    for i, data in ipairs(getSchema()) do
        schema[data.name] = {"table", {}}
    end
end

generateSchema()

UserData.schema = schema

function UserData:ctor()
    local properties = {}
    for i, data in ipairs(getSchema()) do
        properties[data.name] = data.module.new()
    end
    UserData.super.ctor(self, properties)
end

function UserData:init()
    for i, data in ipairs(getSchema()) do
        local g = "get"
        local gkey = g .. data.name:ucfirst()
        local getFunc = self[gkey]
        if getFunc then
            local fieldData = getFunc(self)
            if fieldData.init then
                fieldData:init()
            end
        end
    end
end

function UserData:clear()
    for i, data in ipairs(schema) do
        local g = "get"
        local gkey = g .. field:ucfirst()
        local getFunc = self[gkey]
        if getFunc then
            local fieldData = getFunc(self)
            if fieldData.clear then
                fieldData:clear()
            end
        end
    end
    self:save()
end

function UserData:save()
    for field, _ in pairs(schema) do
        local g = "get"
        local gkey = g .. field:ucfirst()
        local getFunc = self[gkey]
        if getFunc then
            local fieldData = getFunc(self)
            if fieldData.save then
                fieldData:save()
            end
        end
    end
end

function UserData:getRandomList(nums)
    nums = nums >= 100 and 100 or nums
    nums = nums <= 0 and 0 or nums
    local randomMaps = {}
    for i = 1, 100 do
        table.insert(randomMaps, i)
    end
    local result = {}
    for i = 1, nums do
        local index = math.random(1, #randomMaps)
        result[randomMaps[index]] = true
        table.remove(randomMaps, 1)
    end
    return result
end

return UserData
